package com.isoft.game.sys.user.controller;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.isoft.game.common.constant.MessageConstant;
import com.isoft.game.common.controller.PaginationController;
import com.isoft.game.common.message.AjaxResponse;
import com.isoft.game.common.model.PersistModel;
import com.isoft.game.common.util.EncoderByMd5;
import com.isoft.game.common.util.FamilyLogger;
import com.isoft.game.sys.user.entity.User;
import com.isoft.game.sys.user.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController extends PaginationController<User> {

    @Resource
    public UserService userServiceImpl;

    @RequestMapping("login")
    @ResponseBody
	public AjaxResponse userLogin(User user, AjaxResponse ajaxResponse, HttpSession session){
		User param = userServiceImpl.queryUserByName(user);
		if (param!=null) {
			try {
				if (EncoderByMd5.encoderByMd5(user.getPassword()).equals(param.getPassword())) {
					session.setAttribute("user", param);
					ajaxResponse.setSuccessMessage(MessageConstant.MESSAGE_ALERT_SUCCESS,user.getBusinessId());
				}else{
					ajaxResponse.setErrorMessage(MessageConstant.MESSAGE_ALERT_ERROR,"用户名或密码不正确。");
				}
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			ajaxResponse.setErrorMessage(MessageConstant.MESSAGE_ALERT_ERROR,"用户名或密码不正确。");
		}
		return ajaxResponse;
	}
	
    /**
     * 增加
     * @param
     * @return
     */
    @RequestMapping("register")
    @ResponseBody
    public AjaxResponse userRegister(User user, AjaxResponse ajaxResponse, HttpSession session){
    	if(null!=userServiceImpl.queryUserByName(user)){
    		PersistModel persistModel = userServiceImpl.persistUser(user);
            if(persistModel.isSuccessBySinglePersist()){
            	ajaxResponse.setSuccessMessage(MessageConstant.MESSAGE_ALERT_SUCCESS,user.getBusinessId());
            }else{
            	ajaxResponse.setErrorMessage(MessageConstant.MESSAGE_ALERT_ERROR,"注册失败");
            }
    	}else{
    		ajaxResponse.setErrorMessage(MessageConstant.MESSAGE_ALERT_ERROR,"用户名已经注册");
    	}
    	return ajaxResponse;
    }
    
    @RequestMapping("exitSystem")
    @ResponseBody
	public ModelAndView  exitSystem(ModelAndView model, HttpSession session){
		User user = (User) session.getAttribute("user");
		FamilyLogger.sysInfo("当前登陆人" + user.getBusinessId(),"当前登陆人" + user.getUsername() + "退出了系统");
		session.removeAttribute("user");
		session.invalidate();
		model.setViewName("redirect:/view/background/login");
		return model;
	}
}
